from datetime import timedelta
from flask import Flask
from flask_cors import CORS
from config import db_config, jwt_config, mail_config
from controller.email_controller import email_api
from controller.user_controller import user_api
from controller.yolo_controller import yolo_api
from controller.file_controller import file_api
from controller.detect_result_controller import detect_result_api
from common.database import db
from flask_jwt_extended import JWTManager
from common.mail import mail
from common.my_socketio import socketio
# from ultralytics import YOLOv10


app = Flask(__name__)

# 跨域访问
CORS(app)

# 设置 Flask-JWT-Extended 扩展
app.config["JWT_SECRET_KEY"] = jwt_config.secret_key
jwt = JWTManager(app)
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(hours=jwt_config.jwt_time)


app.register_blueprint(yolo_api, url_prefix='/yolo')
# app.register_blueprint(register_api, url_prefix='/register')
# app.register_blueprint(login_api, url_prefix='/login')
app.register_blueprint(user_api, url_prefix='/user')
app.register_blueprint(file_api, url_prefix='/file')
app.register_blueprint(email_api, url_prefix='/email')
app.register_blueprint(detect_result_api, url_prefix='/detect_result')

# app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@localhost:3308/yolo'
# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

# 配置邮箱
app.config['MAIL_SERVER'] = mail_config.MAIL_SERVER
app.config['MAIL_PORT'] = mail_config.MAIL_PORT
app.config['MAIL_USERNAME'] = mail_config.MAIL_USERNAME
app.config['MAIL_PASSWORD'] = mail_config.MAIL_PASSWORD
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USE_SSL'] = False

# app.config['SECRET_KEY'] = secret_key_config.SECRET_KEY
# socketio = SocketIO(app, cors_allowed_origins="*")
socketio.init_app(app, cors_allowed_origins="*")

# mail = Mail(app)

# mail.Mail(app)

mail.init_app(app)

app.config.from_object(db_config)

db.init_app(app)

# model = YOLOv10(r'D:\24926\Desktop\MyProject\thesis\yolov10-main\pt\yolov10n.pt')

# import DDNS
# DDNS.run()

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=12138, debug=True)
    # app.run(host="::", port=12138, debug=True)
    # socketio.run(app, debug=True, host='0.0.0.0', port=5000)
